package remote.controls
{
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import remote.enums.ScrollPolicy;
	import remote.events.ResizeEvent;
	import remote.events.SliderEvent;
	import remote.geom.EdgeMetrics;
	import remote.geom.Size;
	
	
	/**
	 * 滚动容器
	 * @author 银魂飞雪
	 * @createDate 2011-3-25
	 */
	public class Scroller extends Component
	{
		
		private var _content:Component;
		private var _hscroller:HSlider;
		private var _vscroller:VSlider;
		
		private var _scrollPower:Number = 10;
		private var _hscrollPolicy:String = ScrollPolicy.AUTO;
		private var _vscrollPolicy:String = ScrollPolicy.AUTO; 
		public function Scroller()
		{
			super();
		}
		
		override public function updateLayout():void
		{
			super.updateLayout();
			if(_content != null)
			{
				var edge:EdgeMetrics = layout.edge;
				_content.move(edge.left, edge.top);
				scroll(scrollX, scrollY);
			}
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		override protected function createChildren():void
		{
			super.createChildren();
			_hscroller = new HSlider();
			_hscroller.liveDragging = true;
			_hscroller.addEventListener(SliderEvent.CHANGE, hscroller_changeHandler);
			
			_vscroller = new VSlider();
			_vscroller.liveDragging = true;
			_vscroller.addEventListener(SliderEvent.CHANGE, vscroller_changeHandler);
			super.addChild(_hscroller);
			super.addChild(_vscroller);
			updateScroller();
			updateChildren();
			
		}
		
		override protected function addHandlers():void
		{
			super.addHandlers();
			addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */			
		override protected function updateChildren():void
		{
			if(_hscroller.visible)
			{
				if(_vscroller.visible)
					_hscroller.width = width - _vscroller.width;
				else
					_hscroller.width = width;
				_hscroller.y = height - _hscroller.height;
			}
			if(_vscroller.visible)
			{
				if(_hscroller.visible)
					_vscroller.height = height - _hscroller.height;
				else
					_vscroller.height= height;
				_vscroller.x = width - _vscroller.width;
			}
		}
		
		/**
		 * 滚动 
		 * @param x
		 * @param y
		 * 
		 */		
		public function scroll(x:Number, y:Number):void
		{
			if(_content != null)
			{
				var edge:EdgeMetrics = layout.edge;
				_content.scrollRect = new Rectangle(x, y, width - edge.left - edge.right, height - edge.top - edge.bottom);
				if(_hscroller.value != (scrollX / maxScrollX * _hscroller.maxValue))
				{
					_hscroller.removeEventListener(SliderEvent.CHANGE, hscroller_changeHandler);
					_hscroller.value = (scrollX / maxScrollX * _hscroller.maxValue);
					_hscroller.addEventListener(SliderEvent.CHANGE, hscroller_changeHandler);
				}
				if(_vscroller.value != (scrollY / maxScrollY * _vscroller.maxValue))
				{
					_vscroller.removeEventListener(SliderEvent.CHANGE, vscroller_changeHandler);
					_vscroller.value = (scrollY / maxScrollY * _vscroller.maxValue);
					_vscroller.addEventListener(SliderEvent.CHANGE, vscroller_changeHandler);
				}
			}
		}
		
		private function updateScroller():void
		{
			
			if(_content == null)
			{
				_hscroller.value = 0; 
				_hscroller.maxValue = 0;
				_vscroller.value = 0; 
				_vscroller.maxValue = 0;
			}
			else
			{
				//先判断是否需要显示滚动条
				var edge:EdgeMetrics = layout.edge;
				if(this._hscrollPolicy == ScrollPolicy.ON || 
					(_hscrollPolicy == ScrollPolicy.AUTO && _content.width > width - edge.left - edge.right))
				{
					_hscroller.visible = true;
					_hscroller.value = scrollX;
					_hscroller.maxValue = this.maxScrollX;
				}
				else
				{
					_hscroller.visible = false;
					_hscroller.value = 0;
					_hscroller.maxValue = 0;
				}
				if(_vscrollPolicy == ScrollPolicy.ON || 
					(_vscrollPolicy == ScrollPolicy.AUTO && _content.height > height - edge.top - edge.bottom))
				{
					_vscroller.visible = true;
					_vscroller.value = scrollY;
					_vscroller.maxValue = this.maxScrollY;
				}
				else
				{
					_vscroller.visible = false;
					_vscroller.value = 0;
					_vscroller.maxValue = 0;
				}
			}
		}
		
		override public function updateSize():void
		{
			var oldSize:Size = new Size(width, height);
			super.updateSize();
			if(!oldSize.equal(new Size(width, height)))
			{
				this.scrollRect = new Rectangle(0, 0, width, height);
				this.updateScroller();
				this.updateChildren();
			}
		}
		
		private function mouseWheelHandler(e:MouseEvent):void
		{
			var scrollTo:Number = scrollY - e.delta * _scrollPower;
			if(scrollTo < 0)
				scrollTo = 0;
			if(scrollTo > maxScrollY)
				scrollTo = maxScrollY;
			if(scrollTo != this.scrollY)
				this.scroll(scrollX, scrollTo);
		}
		
		private function hscroller_changeHandler(e:SliderEvent):void
		{
			scroll(e.value, scrollY);
		}
		
		private function vscroller_changeHandler(e:SliderEvent):void
		{
			scroll(scrollX, e.value);
		}
		
		override public function addChild(child:DisplayObject):DisplayObject
		{
			throw new Error("请通过content设置内容子项");
		}
		
		override public function addChildAt(child:DisplayObject, index:int):DisplayObject
		{
			throw new Error("请通过content设置内容子项");
		}
		
		private function content_resizeHandler(e:ResizeEvent):void
		{
			updateScroller();
			updateChildren();
		}
		/**
		 * 滚动的内容，只允许设置一个。
		 * 
		 */		
		public function get content():Component
		{
			return _content;
		}
		
		public function set content(value:Component):void
		{
			if(value != _content)
			{
				if(_content != null)
				{
					_content.removeEventListener(ResizeEvent.RESIZE, content_resizeHandler);
					super.removeChild(_content);
				}
				_content = value;
				if(_content != null)
				{
					scroll(0, 0);
					_content.addEventListener(ResizeEvent.RESIZE, content_resizeHandler);
					super.addChildAt(_content, 0);
				}
				this.updateScroller();
				updateChildren();
			}
			
		}
		
		/**
		 * 水平方向滚动位置 
		 * 
		 */		
		public function get scrollX():Number
		{
			if(_content == null)
				return 0
			else
				return _content.scrollRect.x;
		}
		
		/**
		 * Y方向滚动位置 
		 * 
		 */		
		public function get scrollY():Number
		{
			if(_content == null)
				return 0
			else
				return _content.scrollRect.y;
		}
		
		/**
		 * X方向最大可滚动范围 
		 * 
		 */		
		public function get maxScrollX():Number
		{
			if(_content == null)
				return 0;
			else
				if(_vscroller.visible)
					return Math.max(0, _content.width - (width - this._vscroller.width));
			return Math.max(0, _content.width - width);
		}
		
		/**
		 * Y方向最大可滚动范围 
		 * 
		 */		
		public function get maxScrollY():Number
		{
			if(_content == null)
				return 0;
			else
				if(_hscroller.visible)
					return Math.max(0, _content.height - (height - this._hscroller.height));
			return Math.max(0, _content.height - height);
		}
		
		/**
		 * 使用鼠标滚轮时的滚动倍率 
		 * 
		 */		
		public function get scrollPower():Number
		{
			return _scrollPower;
		}
		
		public function set scrollPower(value:Number):void
		{
			_scrollPower = value;
		}
		
		public function get vscroller():VSlider
		{
			return _vscroller;
		}
		
		public function get hscroller():HSlider
		{
			return _hscroller;
		}

		public function get hscrollPolicy():String
		{
			return _hscrollPolicy;
		}

		public function set hscrollPolicy(value:String):void
		{
			_hscrollPolicy = value;
		}

		public function get vscrollPolicy():String
		{
			return _vscrollPolicy;
		}

		public function set vscrollPolicy(value:String):void
		{
			_vscrollPolicy = value;
		}
		
		
	}
}